package com.tencent.qf.bascic.service.impl;

import com.tencent.qf.bascic.controller.req.AwradProfitFlowRequest;
import com.tencent.qf.bascic.dal.AwardProfitFlowDO;
import com.tencent.qf.bascic.service.DataSearchService;
import com.tencent.qf.common.pojo.PageRequest;
import com.tencent.qf.common.pojo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DataSearchServiceImpl implements DataSearchService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 待确认收益流水
     *
     * @param request
     * @return
     */
    @Override
    public PageResult<AwardProfitFlowDO> awradProfitFlow(PageRequest<AwradProfitFlowRequest> request) {
        AwradProfitFlowRequest requestData = request.getData();
        // 构造排序信息
        SortBuilder sortBuilder = new FieldSortBuilder("createTm")
                .order(SortOrder.DESC);

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.mustNot(QueryBuilders.existsQuery("deletedTm"));
        List<QueryBuilder> mustQueryList = new ArrayList<>();
        if(StringUtils.isNotEmpty(requestData.getUid())){
            mustQueryList.add(QueryBuilders.matchPhraseQuery("uid", requestData.getUid()));
        }
        // 时间范围查询
        if(StringUtils.isNotEmpty(requestData.getCreateTmStart())){
            mustQueryList.add(QueryBuilders.rangeQuery("createTm").gte(requestData.getCreateTmStart()));
        }
        if(StringUtils.isNotEmpty(requestData.getCreateTmEnd())){
            mustQueryList.add(QueryBuilders.rangeQuery("createTm").lte(requestData.getCreateTmEnd()));
        }
        // 来源类型搜索
        if(requestData.getSources() != null && requestData.getSources().length > 0){
            mustQueryList.add(QueryBuilders.termsQuery("source", requestData.getSources()));
        }
        // 分销单号
        if(StringUtils.isNotEmpty(requestData.getPlainId())){
            mustQueryList.add(QueryBuilders.matchPhraseQuery("plainOrderId", requestData.getPlainId()));
        }
        // 任务ID
        if(StringUtils.isNotEmpty(requestData.getTaskId())){
            mustQueryList.add(QueryBuilders.matchPhraseQuery("taskId", requestData.getTaskId()));
        }
        // 子任务ID
        if(StringUtils.isNotEmpty(requestData.getSubTask())){
            mustQueryList.add(QueryBuilders.matchPhraseQuery("subTask", requestData.getSubTask()));
        }
        boolQueryBuilder.must().addAll(mustQueryList);

//        // 分销单号/任务ID
//        if(StringUtils.isNotEmpty(requestData.getPlainId())){
//            List<QueryBuilder> shouldQueryList = new ArrayList<>();
//            shouldQueryList.add(QueryBuilders.matchPhraseQuery("plainOrderId", requestData.getPlainId()));
//            shouldQueryList.add(QueryBuilders.matchPhraseQuery("taskId", requestData.getPlainId()));
//            BoolQueryBuilder shouldQueryBuilder = QueryBuilders.boolQuery();
//            shouldQueryBuilder.should().addAll(shouldQueryList);
//            mustQueryList.add(shouldQueryBuilder);
//        }
//        boolQueryBuilder.must().addAll(mustQueryList);

        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(org.springframework.data.domain.PageRequest.of(request.getPageNo() - 1, request.getPageSize()))
                .withSort(sortBuilder)
                .build();

        SearchHits<AwardProfitFlowDO> searchHits = elasticsearchRestTemplate.search(query, AwardProfitFlowDO.class);

        List<AwardProfitFlowDO> collect = searchHits.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());

        PageResult<AwardProfitFlowDO> pageResult = new PageResult<>(collect, searchHits.getTotalHits(), request.getPageNo(), request.getPageSize());
        return pageResult;
    }
}
